package refactoringtechniques;

import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class Main {

    public static void main(String[] args) {
        MainForm mainForm = new MainForm();
    }

    private static class MainForm extends JFrame {
        private JLabel label1 = new JLabel();
        private JLabel label2 = new JLabel();
        private JComboBox codeSmellGroups = new JComboBox();
        private JComboBox codeSmells = new JComboBox();
        private JList refactorings = new JList();

        public MainForm() {
            makeInterface();
            initialize();
        }

        private void makeInterface() {

            Font theFont = new Font(Font.SANS_SERIF, Font.PLAIN, 11);
            Container theContent;

            this.setTitle("Refactoring Techniques");
            this.setSize(442, 300);
            this.setLayout(null);
            this.setVisible(true);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setResizable(false);
            theContent = this.getContentPane();

            label1.setText("Code Smell Group");
            label1.setFont(theFont);
            label1.setLocation(12, 15);
            label1.setSize(92, 13);
            theContent.add(label1);

            label2.setText("Code Smell");
            label2.setFont(theFont);
            label2.setLocation(44, 42);
            label2.setSize(60, 13);
            theContent.add(label2);

            refactorings.setModel(new DefaultListModel());
            refactorings.setFont(theFont);
            refactorings.setLocation(12, 66);
            refactorings.setSize(410, 199);
            theContent.add(refactorings);

            codeSmellGroups.setModel(new DefaultComboBoxModel());
            codeSmellGroups.setFont(theFont);
            codeSmellGroups.setLocation(110, 12);
            codeSmellGroups.setSize(312, 21);
            codeSmellGroups.addActionListener(new GeneralComboBoxListener(codeSmells, codeSmellGroups, refactorings));
            theContent.add(codeSmellGroups);

            codeSmells.setModel(new DefaultComboBoxModel());
            codeSmells.setFont(theFont);
            codeSmells.setLocation(110, 39);
            codeSmells.setSize(312, 21);
            codeSmells.addActionListener(new GeneralComboBoxListener(codeSmells, codeSmellGroups, refactorings));
            theContent.add(codeSmells);

            this.repaint();
        }

        private void initialize() {
            codeSmellGroups.addItem("");
            codeSmellGroups.addItem("Bloaters");
            codeSmellGroups.addItem("Object-Orientation Abusers");
            codeSmellGroups.addItem("Change Preventers");
            codeSmellGroups.addItem("Dispensables");
            codeSmellGroups.addItem("Couplers");
        }
    }

    private static class GeneralComboBoxListener implements ActionListener {

        private JComboBox codeSmells;
        private JComboBox codeSmellGroups;
        private DefaultListModel refactorings;

        public GeneralComboBoxListener(JComboBox codeSmells, JComboBox codeSmellGroups, JList refactorings) {
            if(codeSmells != null)
                this.codeSmells = codeSmells;

            if(codeSmellGroups != null)
                this.codeSmellGroups = codeSmellGroups;

            if(refactorings != null)
                this.refactorings = (DefaultListModel)refactorings.getModel();
        }

        public void actionPerformed(ActionEvent e) {
            JComboBox sorz = (JComboBox)e.getSource();
            if(sorz.equals(codeSmellGroups)) {
                if(codeSmellGroups.getSelectedItem().equals("Bloaters")) {
                    codeSmells.removeAllItems();
                    refactorings.clear();
                    codeSmells.addItem("");
                    codeSmells.addItem("Long Method");
                    codeSmells.addItem("Large Class");
                    codeSmells.addItem("Primitive Obsession");
                    codeSmells.addItem("Long Parameter List");
                    codeSmells.addItem("DataClumps");
                } else if(codeSmellGroups.getSelectedItem().equals("Object-Orientation Abusers")) {
                    codeSmells.removeAllItems();
                    refactorings.clear();
                    codeSmells.addItem("");
                    codeSmells.addItem("Switch Statements");
                    codeSmells.addItem("Temporary Field");
                    codeSmells.addItem("Refused Request");
                    codeSmells.addItem("Alternative Class with Different Interface");
                } else if(codeSmellGroups.getSelectedItem().equals("Change Preventers")) {
                    codeSmells.removeAllItems();
                    refactorings.clear();
                    codeSmells.addItem("");
                    codeSmells.addItem("Divergent Change");
                    codeSmells.addItem("Shotgun Surgery");
                    codeSmells.addItem("Parallel Inheritance Hierarchies");
                } else if(codeSmellGroups.getSelectedItem().equals("Dispensables")) {
                    codeSmells.removeAllItems();
                    refactorings.clear();
                    codeSmells.addItem("");
                    codeSmells.addItem("Lazy Class");
                    codeSmells.addItem("Data Class");
                    codeSmells.addItem("Duplicate Code");
                    codeSmells.addItem("Dead Code");
                    codeSmells.addItem("Speculative Generality");
                } else if(codeSmellGroups.getSelectedItem().equals("Couplers")) {
                    codeSmells.removeAllItems();
                    refactorings.clear();
                    codeSmells.addItem("");
                    codeSmells.addItem("Feature Envy");
                    codeSmells.addItem("Inappropriate Intimacy");
                    codeSmells.addItem("Message Chains");
                    codeSmells.addItem("Middle Man");
                }
            } else if(sorz.equals(codeSmells)) {
                if (codeSmellGroups.getSelectedItem().equals("Bloaters")) {
                    if (codeSmells.getSelectedItem() == null) {
                    } else if (codeSmells.getSelectedItem().equals("Long Method")) {
                        refactorings.clear();
                        refactorings.addElement("Extract Method");
                        refactorings.addElement("Introduce Parameter Object");
                        refactorings.addElement("Decompose Conditional If");
                        refactorings.addElement("Preserve Whole Object");
                        refactorings.addElement("Replace Method with Method Object");
                        refactorings.addElement("Replace Temp with Query");
                    } else if (codeSmells.getSelectedItem().equals("Large Class")) {
                        refactorings.clear();
                        refactorings.addElement("Extract Class");
                        refactorings.addElement("Extract Subclass");
                        refactorings.addElement("Extract Interface");
                        refactorings.addElement("Replace Data Value with Object");
                    } else if (codeSmells.getSelectedItem().equals("Primitive Obsession")) {
                        refactorings.clear();
                        refactorings.addElement("Replace Data Value with Objects");
                        refactorings.addElement("Introduce Parameter Object");
                        refactorings.addElement("Extract Class");
                        refactorings.addElement("Replace Type Code with Class");
                        refactorings.addElement("Replace Type Code with Subclass");
                        refactorings.addElement("Replace Array with Object");
                    } else if (codeSmells.getSelectedItem().equals("Long Parameter List")) {
                        refactorings.clear();
                        refactorings.addElement("Replace Parameter with Method");
                        refactorings.addElement("Introduce Parameter Object");
                        refactorings.addElement("Preserve Whole Object");
                    } else if (codeSmells.getSelectedItem().equals("DataClumps")) {
                        refactorings.clear();
                        refactorings.addElement("Extract Class");
                        refactorings.addElement("Preserve Whole Object");
                        refactorings.addElement("Introduce Parameter Object");
                    }
                } else if (codeSmellGroups.getSelectedItem().equals("Object-Orientation Abusers")) {
                    if (codeSmells.getSelectedItem() == null) {
                    } else if (codeSmells.getSelectedItem().equals("Switch Statements")) {
                        refactorings.clear();
                        refactorings.addElement("Replace Conditional with Polymorphism");
                        refactorings.addElement("Replace Type Code with Subclass");
                        refactorings.addElement("Replace Type Code with State/Strategy");
                        refactorings.addElement("Replace Parameter with Explicit Methods");
                    } else if (codeSmells.getSelectedItem().equals("Temporary Field")) {
                        refactorings.clear();
                        refactorings.addElement("Extract Class");
                    } else if (codeSmells.getSelectedItem().equals("Refused Request")) {
                        refactorings.clear();
                        refactorings.addElement("Push Down Field");
                        refactorings.addElement("Push Down Method");
                        refactorings.addElement("Replace Inheritance with Delegation");
                    } else if (codeSmells.getSelectedItem().equals("Alternative Class with Different Interface")) {
                        refactorings.clear();
                        refactorings.addElement("Rename Method");
                        refactorings.addElement("Move Method");
                    }
                } else if (codeSmellGroups.getSelectedItem().equals("Change Preventers")) {
                    if (codeSmells.getSelectedItem() == null) {
                    } else if (codeSmells.getSelectedItem().equals("Divergent Change")) {
                        refactorings.clear();
                        refactorings.addElement("Extract Class");
                    } else if (codeSmells.getSelectedItem().equals("Shotgun Surgery")) {
                        refactorings.clear();
                        refactorings.addElement("Move Method");
                        refactorings.addElement("Move Field");
                        refactorings.addElement("Inline Class");
                    } else if (codeSmells.getSelectedItem().equals("Parallel Inheritance Hierarchies")) {
                        refactorings.clear();
                        refactorings.addElement("Move Method");
                        refactorings.addElement("Move Field");
                    }
                } else if (codeSmellGroups.getSelectedItem().equals("Dispensables")) {
                    if (codeSmells.getSelectedItem() == null) {
                    } else if (codeSmells.getSelectedItem().equals("Lazy Class")) {
                        refactorings.clear();
                        refactorings.addElement("Collapse Hierarchy");
                        refactorings.addElement("Inline Class");
                        refactorings.addElement("Inline Singleton");
                    } else if (codeSmells.getSelectedItem().equals("Data Class")) {
                        refactorings.clear();
                        refactorings.addElement("Move Method");
                        refactorings.addElement("Encapsulate Field");
                        refactorings.addElement("Encapsulate Collection");
                    } else if (codeSmells.getSelectedItem().equals("Duplicate Code")) {
                        refactorings.clear();
                        refactorings.addElement("Extract Method");
                    } else if (codeSmells.getSelectedItem().equals("Dead Code")) {
                        refactorings.clear();
                        refactorings.addElement("Source Control");
                        refactorings.addElement("Delete It");
                    } else if (codeSmells.getSelectedItem().equals("Speculative Generality")) {
                        refactorings.clear();
                        refactorings.addElement("Collapse Hierarchy");
                        refactorings.addElement("Rename Method");
                        refactorings.addElement("Remove Parameter");
                        refactorings.addElement("Inline Class");
                    }
                } else if (codeSmellGroups.getSelectedItem().equals("Couplers")) {
                    if (codeSmells.getSelectedItem() == null) {
                    } else if (codeSmells.getSelectedItem().equals("Feature Envy")) {
                        refactorings.clear();
                        refactorings.addElement("Extract Method");
                        refactorings.addElement("Move Method");
                        refactorings.addElement("Move Field");
                    } else if (codeSmells.getSelectedItem().equals("Inappropriate Intimacy")) {
                        refactorings.clear();
                        refactorings.addElement("Move Method");
                        refactorings.addElement("Move Field");
                        refactorings.addElement("Change Bidirectional Association to Unidirectional Association");
                        refactorings.addElement("Extract Class");
                        refactorings.addElement("Extract Delegate");
                        refactorings.addElement("Replace Inheritance with Delegation");
                    } else if (codeSmells.getSelectedItem().equals("Message Chains")) {
                        refactorings.clear();
                        refactorings.addElement("Hide Delegate");
                        refactorings.addElement("Extract Method");
                        refactorings.addElement("Move Method");
                    } else if (codeSmells.getSelectedItem().equals("Middle Man")) {
                        refactorings.clear();
                        refactorings.addElement("Remove Middle Man");
                        refactorings.addElement("Inline Method");
                        refactorings.addElement("Replace Delegation with Inheritance");
                    }
                }
            }
        }
    }
}